package com.vito.oa.act.action;

import java.text.SimpleDateFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.MyHashMap;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.extend.model.OaActExApproveInfoModel;
import com.vito.oa.act.extend.model.OaActExBussModel;
import com.vito.oa.act.service.OaActGeneralOperateService;

/**
 * 审批单-撤回/删除/归档
 * @author xzn
 *
 */
@Controller
@RequestMapping("oa/act/generalOperate")
public class OaActGeneralOperateAction extends BaseAction {

	private static final Logger log = LoggerFactory.getLogger(OaActGeneralOperateAction.class);
	
	@Reference
	private OaActGeneralOperateService oaActGeneralOperateService;
	/**
	 * 获取企业拥有的流程列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getProcessList")
	public Result getProcessListByDept(OaActExBussModel model){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			String deptId = user.getDeptParentId();
			model.setCreateDept(deptId);;
			List<MyHashMap<String, String>> processList = oaActGeneralOperateService.queryProcessListByDept(model);
			rs.setData(processList);
			rs.setCode(Result.CODE_OK);
		}catch(Exception e){
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
		return rs;
	}
	/**
	 * 申请撤回
	 * @param businessId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/withdraw")
	public Result withdrawProcess(String businessId){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			rs = oaActGeneralOperateService.withdrawPersonProcess(businessId);
		}catch(Exception e){
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
		}
		return rs;
	}
	/**
	 * 前端根据撤回条件判断是否显示撤回按钮
	 * @param businessId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getWithdrawCondition")
	public Result getWithdrawCondition(String businessId){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			rs.setCode(Result.CODE_OK);
			rs.setData(oaActGeneralOperateService.canBeWithdraw(businessId));
		}catch(Exception e){
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
		}
		return rs;
	}
	/**
	 * 审批删除
	 * @param businessId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public Result deleteProcessData(String businessId){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}			
			if(oaActGeneralOperateService.deleteUserProcess(businessId)){
				rs.setCode(Result.CODE_OK);
				rs.setMsg("删除成功");
			}else{
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("删除失败");
			}
		}catch(Exception e){
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
		}
		return rs;
	}
	/**
	 * 审批归档
	 * @param businessId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/filed")
	public Result processFiled(String businessId) { 
		Result rs = new Result();
		try {
			UserModel printUser = SessionMap.getObj();
			if (null == printUser) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (oaActGeneralOperateService.updateProcessToFiled(businessId)) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("归档成功");
			} else {
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("归档失败");
			}
		} catch (Exception e) {
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
		}

		return rs;
	}
	/**
	 * 分页查询删除的/归档的审批单
	 * <pre>
	 * dataStatus=1:删除的审批
	 * dataStatus=2:归档的审批
	 * </pre>
	 * @param actExApproveInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryDeletedOrFiledByPage")
	public Result queryDeletedOrFiledByPage(OaActExApproveInfoModel actExApproveInfoModel){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}	
			actExApproveInfoModel.setUserid(user.getUserId());
			//默认取审批模块
			if(Validata.isNullOrEmpty(actExApproveInfoModel.getMoudelType())){
				actExApproveInfoModel.setMoudelType(OaActContants.PROCESS_MOUDEL_TYPE_APPROVAL);
			}
			if(!Validata.isNullOrEmpty(actExApproveInfoModel.getEndtime())){
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				actExApproveInfoModel.setEndtime(df.format(df.parse(actExApproveInfoModel.getEndtime()))+" 23:59:59");
			}	
			rs.setData(oaActGeneralOperateService.queryDeletedOrFiledData(actExApproveInfoModel));
			rs.setCode(Result.CODE_OK);
		}catch(Exception e){
			log.error("查询异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
}
